const QuoteApi = require('../../../api/quote.js');
const util = require('../../../utils/util.js');
const commonUtil = require('../../../utils/common.js');

Page({
  data: {
    detailModel: {
      commodity: {},
    },
    tableInfo: {
      columns: [
        {label: '日期', prop: 'quoteDate'},
        {label: '价格', prop: 'price', align: 'right'},
        {label: '均价', prop: 'averagePrice', align: 'right'},
        {label: '涨跌', prop: 'upsDowns', align: 'right'},
      ],
      data: [],
      inQuoteDetail: true
    },
    optionsData: {
      quoteDate: null,
      quoteId: null
    }
  },

  onLoad(options) {
    if (options.quoteId) {
      this.setData({
        ['optionsData.quoteDate']: options.quoteDate,
        ['optionsData.quoteId']: options.quoteId,
      })
    }
  },

  onShow() {
    let currentQuote = wx.getStorageSync('currentQuote');
    if (currentQuote) {
      this.getFrontQuoteDetail()
    } else if (this.data.optionsData.quoteId) {
      this.getFrontQuoteDetail(this.data.optionsData.quoteDate, this.data.optionsData.quoteId)
    }
  },

  onHide() {
    this.initData()
  },

  onUnload() {
    console.log('quoteDetail onUnload');
    wx.setStorageSync('detailBack', true);
    // wx.removeStorageSync('currentQuote')
  },

  initData(){
    this.setData({
      detailModel: {
        commodity: {},
      },
      ['optionsData.quoteDate']: null,
      ['optionsData.quoteId']: null,
    })
  },
  // 报价详情查询
  getFrontQuoteDetail(quoteDate, quoteId){
    let currentQuote = wx.getStorageSync('currentQuote');
    let _quoteDate = currentQuote ? currentQuote.quoteDate : quoteDate;
    let _quoteId = currentQuote ? currentQuote.quoteId : quoteId;
    QuoteApi.getFrontQuoteDetail({quoteDate: _quoteDate, quoteId: _quoteId}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        res.data.quoteUnit = util.quoteUnitFilter(res.data.quoteUnit)
        res.data.invoiceTax = util.invoiceTaxFilter(res.data.invoiceTax)
        this.setData({
          detailModel: res.data
        })
        this.getFrontQuotePrice(_quoteId, res.data.quoteType)
      } else {
        if (res.errCode == '999901') {
          let currentQuote = wx.getStorageSync('currentQuote');
          let _optionsData = this.data.optionsData;
          let _quoteDate = _optionsData.quoteDate ? _optionsData.quoteDate : currentQuote ? currentQuote.quoteDate : '';
          let _quoteId = _optionsData.quoteId ? _optionsData.quoteId : currentQuote ? currentQuote.quoteId : '';
          commonUtil.handleTokenExpire('/packageA/pages/quoteDetail/index&quoteDate='+_quoteDate+'&quoteId='+_quoteId, 'link')
        }
      }
    })
  },
  // 查询最近30天报价数据
  getFrontQuotePrice(quoteId, quoteType){
    let currentQuote = wx.getStorageSync('currentQuote')
    let _quoteId = currentQuote ? currentQuote.quoteId : quoteId;
    QuoteApi.getFrontQuotePrice({quoteId: _quoteId, termType: '30d'}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        res.data.forEach(item=>{
          item.quoteType = quoteType
        })
        console.log(res.data);
        this.setData({
          ['tableInfo.data']: res.data
        })
      }
    })
  },
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() {
    let currentQuote = wx.getStorageSync('currentQuote');
    return {
      title: "报价详情",
      path: "/packageA/pages/quoteDetail/index?quoteId="+currentQuote.quoteId+"&quoteDate="+currentQuote.quoteDate,
    }
  },
  onShareTimeline() {
    let currentQuote = wx.getStorageSync('currentQuote');
    return {
      title: "报价详情",
      query: "quoteId="+currentQuote.quoteId+"&quoteDate="+currentQuote.quoteDate,
    }
  }
})